Skip to content

fix: track opencode session switches - #2455

Open
akbash-bot wants to merge 1 commit into
masterfrom
akbash/2450-opencode-session-switch
Open

fix: track opencode session switches#2455
akbash-bot wants to merge 1 commit into
masterfrom
akbash/2450-opencode-session-switch

Conversation

@akbash-bot

@akbash-bot akbash-bot commented Aug 6, 2026

Copy link
Copy Markdown
Collaborator

Summary

  • track the root conversation selected by each OpenCode TUI instead of inferring selection from server-global events
  • keep attached-client activity and root creation from replacing or suppressing the pane's durable session identity
  • preserve server lifecycle sequencing while allowing TUI-local selection to reanchor restore state
  • install the TUI companion through a comment-preserving tui.jsonc entry without blocking OpenCode's config migration

Validation

  • just check
  • live disposable-session coverage with OpenCode 1.17.8 and 1.18.15
  • verified route-only session selection, attached-client activity and root creation isolation, subsequent selection, and restart restore

refs #2450

@coderabbitai

coderabbitai Bot commented Aug 6, 2026

Copy link
Copy Markdown

Review Change Stack

📝 Walkthrough

Walkthrough

OpenCode now tracks the TUI-selected root session through a dedicated plugin. Installation manages the plugin and JSONC configuration. Terminal state distinguishes local selection from server activity and rejects unrelated session reports.

Changes

OpenCode TUI session selection

Layer / File(s) Summary
TUI session reporting
src/integration/assets/opencode/herdr-tui-session.js, src/integration/assets/opencode/herdr-tui-session.test.ts, justfile
The TUI plugin reports selected root sessions, retries during process detection, ignores child sessions, and stops polling on disposal.
Plugin configuration and lifecycle
src/integration/opencode_config.rs, src/integration/targets.rs, src/integration/actions.rs, src/integration/types.rs, src/integration/tests.rs
Installation and uninstallation manage the TUI plugin and tui.jsonc, including validation, JSONC preservation, cleanup errors, and result paths.
Integration status
src/integration/registry.rs, src/cli/integration.rs, src/integration/mod.rs
Status checks the TUI plugin version and configuration. Incomplete current installations report needs repair.
Session ownership and restore lifecycle
src/terminal/state.rs, src/integration/assets/opencode/herdr-agent-state.js, src/integration/assets/opencode/herdr-agent-state.test.ts, src/agent_resume.rs, docs/next/CHANGELOG.md
Local "select" events can establish or reanchor sessions. Server "new" and "resume" activity does not replace local ownership. Reports from mismatched sessions are rejected.

Estimated code review effort: 4 (Complex) | ~45 minutes

Sequence Diagram(s)

sequenceDiagram
  participant OpenCodeTUI
  participant HerdrTUIPlugin
  participant TerminalState
  OpenCodeTUI->>HerdrTUIPlugin: Select root session
  HerdrTUIPlugin->>TerminalState: Report unsequenced "select"
  TerminalState->>TerminalState: Reconcile local session ownership
  OpenCodeTUI->>TerminalState: Send server session activity
  TerminalState-->>OpenCodeTUI: Reject mismatched session report
Loading

Possibly related PRs

  • herdrdev/herdr#2062: Extends the same OpenCode session-ownership and lifecycle tracking logic.

Suggested labels: coderabbit-review

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 51.61% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Title check ✅ Passed The title clearly describes the main change: tracking OpenCode session switches.
Description check ✅ Passed The description directly explains OpenCode session tracking, TUI integration changes, safeguards, and validation.
✨ Finishing Touches 💡 1
📝 Generate docstrings 💡
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch akbash/2450-opencode-session-switch

Comment @coderabbitai help to get the list of available commands.

@akbash-bot akbash-bot added the ai-review Trigger automated AI reviews for pull requests admitted by the PR gate label Aug 6, 2026

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1


ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 9dc12150-e42a-4e5c-b532-02d4ecce4fe4

📥 Commits

Reviewing files that changed from the base of the PR and between 69a07fd and 43f6e7f.

📒 Files selected for processing (5)
  • docs/next/CHANGELOG.md
  • src/integration/assets/opencode/herdr-agent-state.js
  • src/integration/assets/opencode/herdr-agent-state.test.ts
  • src/integration/mod.rs
  • src/terminal/state.rs

Comment thread src/integration/assets/opencode/herdr-agent-state.js Outdated
@greptile-apps

greptile-apps Bot commented Aug 6, 2026

Copy link
Copy Markdown

Greptile Summary

The PR adds pane-local OpenCode session-selection reporting and updates durable session reconciliation so native restore follows the conversation selected in the TUI while rejecting server-global cross-talk.

  • Installs and configures a dedicated OpenCode TUI plugin, with status and uninstall support.
  • Adds the select lifecycle source and reconciles unsequenced local selections after process detection.
  • Stops treating server-global OpenCode creation and resume activity as authoritative session switches.
  • Adds integration, state-transition, configuration, and plugin boundary tests.

Confidence Score: 5/5

The PR appears safe to merge because no blocking failure remains within the eligible follow-up-review scope.

No blocking failure remains.

Important Files Changed

Filename Overview
src/integration/assets/opencode/herdr-tui-session.js Adds pane-local polling of the selected root OpenCode session and reports selection events with bounded retries and lifecycle cleanup.
src/terminal/state.rs Adds OpenCode selection reconciliation, deferred startup anchoring, and cross-talk rejection while preserving existing lifecycle authority.
src/integration/opencode_config.rs Adds JSONC-preserving management of the OpenCode TUI plugin registration.
src/integration/targets.rs Extends OpenCode installation and removal to manage the TUI plugin and its configuration entry.
src/integration/registry.rs Requires both the versioned TUI asset and its configuration entry for the OpenCode integration to be current.
src/integration/assets/opencode/herdr-agent-state.js Stops promoting server-global session creation into an authoritative local session switch.

Sequence Diagram

sequenceDiagram
  participant TUI as OpenCode TUI
  participant Plugin as herdr-tui-session.js
  participant API as Herdr pane API
  participant State as TerminalState
  participant Store as Durable session identity
  TUI->>Plugin: Select root conversation
  Plugin->>Plugin: Poll route and reject child sessions
  Plugin->>API: "report_agent_session(source=select)"
  API->>State: Set selected session reference
  alt OpenCode process detected
    State->>Store: Reanchor durable identity
  else Process not detected yet
    State->>State: Suppress and retain replacement
    State->>Store: Apply replacement after detection
  end
  Note over State: Server-global activity for another root is ignored
Loading

Reviews (4): Last reviewed commit: "fix: track opencode tui session selectio..." | Re-trigger Greptile

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1


ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: c7ae0178-b40a-47ec-ba71-8dd4d1caf285

📥 Commits

Reviewing files that changed from the base of the PR and between 43f6e7f and 782e2f6.

📒 Files selected for processing (2)
  • src/integration/assets/opencode/herdr-agent-state.js
  • src/integration/assets/opencode/herdr-agent-state.test.ts
🚧 Files skipped from review as they are similar to previous changes (1)
  • src/integration/assets/opencode/herdr-agent-state.js

Comment thread src/integration/assets/opencode/herdr-agent-state.test.ts Outdated
@ogulcancelik
ogulcancelik force-pushed the akbash/2450-opencode-session-switch branch from bbcbfc7 to c62f51c Compare August 9, 2026 01:58

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🧹 Nitpick comments (3)
src/integration/assets/opencode/herdr-tui-session.test.ts (1)

112-125: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low value

Consider covering the exhausted-retry branch.

SELECTION_RETRY_DELAYS_MS has three entries. After the last retry, herdr-tui-session.js sets nextReportAt to Number.POSITIVE_INFINITY and stops reporting the same selection. No test asserts that the reporting stops. A test that waits past the final delay and asserts a stable request count would lock in that boundary.

src/terminal/state.rs (2)

1374-1375: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low value

Consider recording the stale session when a selection clears suppression.

selection_can_reconcile removes the suppression entry for the source without inspecting suppressed.reason. Every other removal site distinguishes HookClear from ProcessExit. clear_full_lifecycle_hook_suppression_for_detected_agent records the suppressed session_ref as a stale session before it drops a HookClear entry (lines 1098-1106). This path drops it silently, so a later report that carries the previous session ref is not filtered by full_lifecycle_hook_report_matches_stale_session.

The opencode_cross_talk guard at lines 863-870 rejects that report while the process is present and the anchor differs, so the reachable exposure is small. Recording the stale session here would keep the two paths consistent.

♻️ Suggested change
         if selection_can_reconcile {
-            self.suppressed_full_lifecycle_hook_reports.remove(&source);
+            if let Some(suppressed) = self.suppressed_full_lifecycle_hook_reports.remove(&source) {
+                if let Some(suppressed_ref) = suppressed
+                    .session_ref
+                    .filter(|suppressed_ref| suppressed_ref != &session_ref)
+                {
+                    self.remember_stale_full_lifecycle_hook_session(
+                        source.clone(),
+                        suppressed.agent_label,
+                        suppressed_ref,
+                    );
+                }
+            }
         } else if full_lifecycle_source && unsequenced_selection {

4620-4656: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low value

Consider adding a boundary test for cross-talk while the process is absent.

opencode_cross_talk at lines 863-870 requires process_present. When the process is absent, a report carrying a different session ref still reaches the buffering path at lines 906-953. That asymmetry is intentional, but no test pins it. A test that clears the detected agent and then sends a mismatched sequenced report would document the boundary.


ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: aec6c888-df3f-48b4-b55f-1b0f88080b93

📥 Commits

Reviewing files that changed from the base of the PR and between 782e2f6 and c62f51c.

📒 Files selected for processing (16)
  • docs/next/CHANGELOG.md
  • justfile
  • src/agent_resume.rs
  • src/cli/integration.rs
  • src/integration/actions.rs
  • src/integration/assets/opencode/herdr-agent-state.js
  • src/integration/assets/opencode/herdr-agent-state.test.ts
  • src/integration/assets/opencode/herdr-tui-session.js
  • src/integration/assets/opencode/herdr-tui-session.test.ts
  • src/integration/mod.rs
  • src/integration/opencode_config.rs
  • src/integration/registry.rs
  • src/integration/targets.rs
  • src/integration/tests.rs
  • src/integration/types.rs
  • src/terminal/state.rs
🚧 Files skipped from review as they are similar to previous changes (2)
  • docs/next/CHANGELOG.md
  • src/integration/assets/opencode/herdr-agent-state.js

Comment thread src/integration/tests.rs
Comment thread src/terminal/state.rs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ai-review Trigger automated AI reviews for pull requests admitted by the PR gate

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants